package com.scpii.universal.concurrent.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.scpii.universal.Const.CompileConfig;
import com.scpii.universal.concurrent.executor.HttpClientExecutor;
import com.scpii.universal.util.Conversion;

/**
 * GET 任务
 * 
 * @author yuejunyan
 */

public class GetTask extends Task {
	private static final Logger logger = LoggerFactory.getLogger(GetTask.class);
	private String url = null;

	public GetTask(TaskMsg msg) {
		super(msg);
		StringBuffer sb = new StringBuffer();
		sb.append(CompileConfig.SERVER_URL).append("?cmd=").append(msg.action)
				.append("&data=").append(Conversion.bytesToHexString(msg.data));
		url = sb.toString();
	}

	@Override
	public void run() {
		try {
			logger.info("GET线程请求");
			if (msg == null)
				return;

			byte[] byteArrayResponse = null;
			for (int i = 0; i < 3; i++) {
				try {
					byteArrayResponse = HttpClientExecutor.getInstance()
							.executeGet(url);
				} catch (Exception e) {
				}
				if (byteArrayResponse != null) {
					break;
				}
			}

			if (byteArrayResponse == null) {
				logger.debug("byteArrayResponse is null");
			} else {
				logger.debug("byteArrayResponse.lenght:"
						+ byteArrayResponse.length);
				if (msg.mCallback != null) {
					msg.mCallback.handleMessage(msg.action, byteArrayResponse);
				}
			}
		} catch (Exception e) {

		}

	}

}
